<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像 -->
      <div class="avatar_box">
        <img src="@/assets/logo.png">
      </div>
      <!-- 表单 -->
      <el-form ref="loginRef" :model="loginForm" :rules="rules" label-width="80px" class="Login_from">
        <el-form-item label="手机号" prop="username">
          <el-input v-model="loginForm.username" type="text" prefix-icon="el-icon-user-solid"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="loginForm.password" type="password" prefix-icon="el-icon-apple"></el-input>
        </el-form-item>
        <el-form-item label="身份" prop="identity">
          <template>
            <el-radio-group v-model="loginForm.identity">
              <el-radio :label="1">工作人员</el-radio>
              <el-radio :label="2">用户</el-radio>
            </el-radio-group>
          </template>
        </el-form-item>
        <el-form-item class="btn">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="success" @click="ShowRegister">用户注册</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>

        </el-form-item>
      </el-form>
    </div>

    <el-dialog title=" 用户注册" :visible.sync="addDialogVisible" width="50%" @close="resetAddForm">
      <el-form :model="addForm" :rules="rules" ref="addFormRef" label-width="100px">
        <el-form-item label="姓名" prop="residentName">
          <el-input v-model="addForm.residentName"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="residentPhone">
          <el-input v-model="addForm.residentPhone" type="number"></el-input>
        </el-form-item>
        <el-form-item label="身份证" prop="residentCard">
          <el-input v-model="addForm.residentCard"></el-input>
        </el-form-item>
        <el-form-item label="所在社区" prop="community">
          <el-select v-model="addForm.community" placeholder="请选择所在社区">
            <el-option v-for="item in cityData" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="详细地址" prop="residentAddress">
          <el-input v-model="addForm.residentAddress"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="newPassword">
          <el-input v-model="addForm.newPassword" type="password"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="residentPassword">
          <el-input v-model="addForm.residentPassword" type="password"></el-input>
        </el-form-item>
        <el-form-item label="人口性质">
          <el-radio v-model="addForm.residentState" :label=0>常住人口</el-radio>
          <el-radio v-model="addForm.residentState" :label=1>流动人口</el-radio>
        </el-form-item>
        <el-form-item label="健康状态">
          <el-radio v-model="addForm.healthState" :label=0>健康/未注射疫苗</el-radio>
          <el-radio v-model="addForm.healthState" :label=1>健康/已打第一针</el-radio>
          <el-radio v-model="addForm.healthState" :label=2>健康/已打第二针</el-radio>
          <el-radio v-model="addForm.healthState" :label=3>健康/已打第三针</el-radio>
          <el-radio v-model="addForm.healthState" :label=4>疑似</el-radio>
          <el-radio v-model="addForm.healthState" :label=5>确诊</el-radio>
        </el-form-item>
        <el-form-item label="隔离信息">
          <el-input v-model="addForm.lsolate" :disabled="addForm.healthState<4 "></el-input>
        </el-form-item>
      </el-form>
      <span>
        <el-button @click="resetAddForm">取 消</el-button>
        <el-button type="primary" @click="addResident">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import cityData from '../assets/js/cityData.js'
export default {
  data () {
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        if (this.addForm.newPassword !== '') {
          this.$refs.addFormRef.validateField('residentPassword')
        }
        callback()
      }
    }
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.addForm.newPassword) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      addDialogVisible: false,
      loginForm: {
        username: '110',
        password: '123456',
        identity: 1
      },
      addForm: {
        residentName: '',
        residentPassword: '',
        residentState: 0,
        residentAddress: '',
        residentCard: '',
        residentPhone: '',
        healthState: 0,
        lsolate: '',
        community: '',
        newPassword: ''
      },
      cityData,
      rules: {
        username: [{ required: true, message: '请输入手机号', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
        residentPassword: [{ validator: validatePass2, trigger: 'blur' }],
        newPassword: [
          { required: true, min: 5, max: 14, message: '长度在 5 到 14 个字符', trigger: 'blur' },
          { validator: validatePass, trigger: 'blur' }
        ],
        community: [{ required: true, message: '请选择所在社区', trigger: 'blur' }],
        residentAddress: [{ required: true, message: '请输入居住详细地址', trigger: 'blur' }],
        residentName: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
        residentPhone: [{ required: true, message: '请输入联系电话', trigger: 'blur' },
          { pattern: /^1[3456789]\d{9}$/, message: '手机格式不正确' }
        ]
      }
    }
  },
  methods: {
    // 清空登录信息框值
    resetLoginForm () {
      this.$refs.loginRef.resetFields()
    },
    // 登录
    async login () {
      this.$refs.loginRef.validate(async valid => {
        if (!valid) return
        const result = await this.$store.dispatch('login', this.loginForm)
        if (!result) {
          return this.$message.error('登录失败')
        }
        this.$message.success('登录成功')
        this.$router.push('/home')
      })
    },
    // 注册
    ShowRegister () {
      this.addDialogVisible = true
    },
    // 添加居民
    async addResident () {
      const { data: res } = await this.$http.post('resident/addResident', this.addForm)
      // console.log(res)
      if (res.meta.status !== 200) {
        this.addDialogVisible = false
        return this.$message.error('添加失败' + res.meta.msg)
      }
      // console.log(res)
      this.$message.success('成功')
      this.addDialogVisible = false
      this.getResident()
    },
    resetAddForm () {
      this.$refs.addFormRef.resetFields()
      this.password = ''
      this.addDialogVisible = false
    }
  }
}
</script>

<style lang="less" scoped>
.login_container {
  background-color: skyblue;
  height: 100%;
  position: relative;
}
.login_box {
  width: 450px;
  height: 300px;
  position: absolute;
  left: 50%;
  top: 50%;
  background-color: #fff;
  transform: translate(-50%, -50%);
}

.avatar_box {
  height: 130px;
  width: 130px;
  border-radius: 50%;
  padding: 10px;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  img {
    height: 100%;
    width: 100%;
    border-radius: 50%;
    background: #eee;
  }
}

.btn {
  display: flex;
  justify-content: flex-end;
}
.Login_from {
  position: absolute;
  bottom: 0;
  width: 90%;
  // padding-right: 30px;
}
</style>
